import axios from "axios";
import { ref } from "vue";
import request from "@/api/request";
// 实时监测数据（新增data类型）
/**
   * "type": "data",
      "payload": {
      "date": "2025-07-07 00:34:59", // 采集时间
      "so2_calculated": 50.224998474121094, // 二氧化硫折算
      "so2_measured": 20.09000015258789, // 二氧化硫实测
      "nox_calculated": 48.39699935913086, // 氮氧化物折算
      "nox_measured": 19.358999252319336, // 氮氧化物实测
      "dust_calculated": 9.017200469970703, // 烟尘折算
      "dust_measured": 3.6068999767303467, // 烟尘实测
      "o2": 19.799999237060547, // 氧气
      "exhaust_flow": 159328.4375, // 废气排放量
      "flue_gas_temp": 32.560001373291016, // 烟气温度
      "flue_gas_humidity": 5.710000038146973, // 烟气湿度
      "flue_gas_pressure": -0.08129999786615372, // 烟气压力
      "flue_gas_traffic": 6.142000198364258 // 烟气流量
      }
   */
export const tempInitMonitorData = {
  date: "",
  so2: { calculated: 0, measured: 0 }, // 折算 实测
  nox: { calculated: 0, measured: 0 },
  dust: { calculated: 0, measured: 0 }, // 颗粒物
  o2: 0, // 氧气
  exhaust_flow: 0,
  flue_gas: {
    temp: 0,
    humidity: 0,
    pressure: 0,
    traffic: 0,
  },
};

export const updateMonitorData = (payload) => {
  return {
    date: payload.date, // 日期保持不变
    so2: {
      calculated: parseFloat(payload.so2_calculated.toFixed(2)),
      measured: parseFloat(payload.so2_measured.toFixed(2)),
    },
    nox: {
      calculated: parseFloat(payload.nox_calculated.toFixed(2)),
      measured: parseFloat(payload.nox_measured.toFixed(2)),
    },
    dust: {
      calculated: parseFloat(payload.dust_calculated.toFixed(2)),
      measured: parseFloat(payload.dust_measured.toFixed(2)),
    },
    o2: parseFloat(payload.o2.toFixed(2)),
    exhaust_flow: parseFloat(payload.exhaust_flow.toFixed(2)),
    flue_gas: {
      temp: parseFloat(payload.flue_gas_temp.toFixed(2)),
      humidity: parseFloat(payload.flue_gas_humidity.toFixed(2)),
      pressure: parseFloat(payload.flue_gas_pressure.toFixed(2)),
      traffic: parseFloat(payload.flue_gas_traffic.toFixed(2)),
    },
  };
};

/**
 * 获取实时监测数据
 * @returns
 */
export const useMonitorData = () => {
  const initMonitorData = ref(tempInitMonitorData);
  request.get("/index/data").then((res) => {
    console.log(
      "%c [ res ]-38",
      "font-size:18px; background:#eec58a; color:#ffffce;",
      res
    );
    initMonitorData.value = updateMonitorData(res.data);
  });
  return { initMonitorData };
};
